{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example 04 - Wideband Modulations Dataset\n",
    "This notebook steps through an example of how to use `torchsig` to instantiate a custom, online `WidebandDataset` containing signals with up to 50+ unique classes of modulations. The notebook then plots the signals using `Visualizers` for the Spectrogram representations of the dataset. \n",
    "\n",
    "-------------------------------------------"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Import Libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from torchsig.utils.visualize import MaskClassVisualizer, mask_class_to_outline, complex_spectrogram_to_magnitude\n",
    "from torchsig.transforms.target_transforms import DescToMaskClass\n",
    "from torchsig.datasets.wideband import WidebandModulationsDataset\n",
    "from torchsig.datasets.signal_classes import torchsig_signals\n",
    "from torchsig.transforms.transforms import Spectrogram, Normalize, Compose\n",
    "from torch.utils.data import DataLoader\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----------------------------------\n",
    "### Define Dataset Parameters & Transforms\n",
    "The `WidebandModulationsDataset` inputs a list of modulations to include, and it also inputs the data and target transforms. Below's example includes the full list of all supported modulations, but a subset of this list can be selected to create an easier, more specialized task. The data transforms below are strictly the IQ to spectrogram transformation with a renormalization; however, these can be replaced with a custom composition of augmentations for tailored experiments. The target transform in this example transforms the `SignalMetadata` for each example into a set of masks for each class."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "modulation_list = torchsig_signals.class_list\n",
    "\n",
    "fft_size = 512\n",
    "num_classes = len(modulation_list)\n",
    "num_iq_samples = fft_size * fft_size\n",
    "num_samples = 20\n",
    "\n",
    "data_transform = Compose([\n",
    "    Spectrogram(nperseg=fft_size, noverlap=0, nfft=fft_size, mode='complex'),\n",
    "    Normalize(norm=np.inf, flatten=True),\n",
    "])\n",
    "\n",
    "target_transform = Compose([\n",
    "    DescToMaskClass(num_classes=num_classes, width=fft_size, height=fft_size),\n",
    "])"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "-----------------------------\n",
    "## Instantiate the Wideband Modulations Dataset\n",
    "Using the above options, the `WidebandModulationsDataset` can be instantiated as shown below. Note that when using custom data transforms, the level options should be set to either 0 or 1, where the only difference between these levels is that level 0 results in fewer signal sources per sample than level 1."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wideband_modulations_dataset = WidebandModulationsDataset(\n",
    "    modulation_list=modulation_list,\n",
    "    level=1,\n",
    "    num_iq_samples=num_iq_samples,\n",
    "    num_samples=num_samples,\n",
    "    transform=data_transform,\n",
    "    overlap_prob=.1,\n",
    "    target_transform=target_transform,\n",
    ")\n",
    "\n",
    "data, label = wideband_modulations_dataset[0]\n",
    "print(\"Data shape: {}\".format(data.shape))\n",
    "print(\"Label shape: {}\".format(label.shape))"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "---\n",
    "### Data Plotting\n",
    "After the dataset is instantiated, it can be viewed using the `MaskClassVisualizer` to verify both the transformed data and transformed annotations."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "data_loader = DataLoader(\n",
    "    dataset=wideband_modulations_dataset,\n",
    "    batch_size=16,\n",
    "    shuffle=True,\n",
    ")\n",
    "\n",
    "visualizer = MaskClassVisualizer(\n",
    "    data_loader=data_loader,\n",
    "    visualize_transform=complex_spectrogram_to_magnitude,\n",
    "    visualize_target_transform=mask_class_to_outline,\n",
    "    class_list=modulation_list,\n",
    ")\n",
    "\n",
    "for figure in iter(visualizer):\n",
    "    figure.set_size_inches(16, 16)\n",
    "    plt.show()\n",
    "    break"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
